﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Controls;
using WPFExcelReport;
using WPFExcelReport.Helpers; 
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TART
    {
        #region ==WIDTH===========
        public static readonly DependencyProperty WIDTHProperty = DependencyProperty.Register("WIDTH",
            typeof(string), typeof(TART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWIDTHChanged));
        public string WIDTH
        {
            get
            {
                return (string)GetValue(WIDTHProperty);
            }
            set
            {
                SetValue(WIDTHProperty, value);
                
            }
        }
        private static void OnWIDTHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TART;
            if (src != null)
            {
                src.WIDTH = (string)e.NewValue;
                src.OnWIDTHChg();
            }
        }
        public void OnWIDTHChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["WIDTH"] = WIDTH;
            if (dwMain == null)
            { }
        }
        #endregion
        #region ==CUSTOMERPRICE===========
        public static readonly DependencyProperty CUSTOMERPRICEProperty = DependencyProperty.Register("CUSTOMERPRICE",
            typeof(string), typeof(TART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCUSTOMERPRICEChanged));
        public string CUSTOMERPRICE
        {
            get
            {
                return (string)GetValue(CUSTOMERPRICEProperty);
            }
            set
            {
                SetValue(CUSTOMERPRICEProperty, value);
                OnCUSTOMERPRICEChg();
            }
        }
        private static void OnCUSTOMERPRICEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TART;
            if (src != null)
            {
                src.CUSTOMERPRICE = (string)e.NewValue;
            }
        }
        public void OnCUSTOMERPRICEChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["CUSTOMERPRICE"] = CUSTOMERPRICE;
            if (dwMain == null)
            { }
        }
        #endregion
        #region ==WEIGHT===========
        public static readonly DependencyProperty WEIGHTProperty = DependencyProperty.Register("WEIGHT",
            typeof(string), typeof(TART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWEIGHTChanged));
        public string WEIGHT
        {
            get
            {
                return (string)GetValue(WEIGHTProperty);
            }
            set
            {
                SetValue(WEIGHTProperty, value);
                OnWEIGHTChg();
            }
        }
        private static void OnWEIGHTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TART;
            if (src != null)
            {
                src.WEIGHT = (string)e.NewValue;
            }
        }
        public void OnWEIGHTChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["WEIGHT"] = WEIGHT;
            if (dwMain == null)
            { }
        }
        #endregion

        public TART()
        { 
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_EXART(); 
            this.DataContext = MV;
            InitializeComponent(); 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if(dwMaster==null)
            {
                WIDTH = "";
                WEIGHT = "";
                CUSTOMERPRICE = "";
            }
            else
            {
                WIDTH = dwMaster["WIDTH"].ToString();
                WEIGHT = dwMaster["WEIGHT"].ToString();
                CUSTOMERPRICE = dwMaster["CUSTOMERPRICE"].ToString(); 
            }
        }
        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM == "")
                return;
            dwData = TBMain.NewRow();
            dwData["ARTICLE"] = ORDERNUM.PadRight(29).Substring(5, 24);
            SResult = TMV.QEXART01(dwData);
            MergeData(SResult, true);
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["ARTICLE"] = ORDERNUM.PadRight(25).Substring(5, 20);
            dwData["DESCRIPTION"] = Helper.Left(ORDERNUM.PadRight(30), 30);
            dwData["SUPPLIER"] = SUPPLIER.PadRight(8);
            TMV.InserICC_EXART(dwData);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.LOADBYAUF();
        }
      
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster== null)
                return;
            TMV.UpdateICC_EXART(dwMaster);
            string key = KEY;
            KEY = "";
            KEY = key;
            base.OnSave();
        }
    }
}